﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Touch2Like.DAL;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;

namespace Touch2Like.Web.Touch2Rate
{
    public partial class Result : System.Web.UI.Page
    {
        string reviewid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["reviewid"]))
                reviewid = Utility.Encryption.Decrypt(Server.UrlDecode(Request["reviewid"]));

            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(reviewid))
                {
                    PH_Error.Visible = false;
                    ReviewDAL review = new ReviewDAL();
                    ReviewInfo reviewInfo = review.GetByID(Convert.ToInt32(reviewid));
                    if (reviewInfo != null)
                    {
                        AnswerDAL answer = new AnswerDAL();
                        DataTable dtAnswer = answer.GetByReviewID(Convert.ToInt32(reviewid));

                        UserDAL user = new UserDAL();
                        DataTable dtSetting = user.GetSettingByUserGUID(reviewInfo.UserGUID);
                        WebUtility.LogoOwner = reviewInfo.UserGUID;
                        if (dtSetting.Select("FlagName='Logo'").Length > 0)
                        {
                            img_logo.Src = dtSetting.Select("FlagName='Logo'")[0]["Value"].ToString();
                        }

                        CouponDesignDAL design = new CouponDesignDAL();
                        CouponDesignInfo designInfo = null;

                        img_coupon.Visible = false;
                        if (dtAnswer.Rows.Count > 0 && dtAnswer.Select("Answer<>'' and Answer is not null").Length == dtAnswer.Rows.Count)
                        {
                            if (dtSetting.Select("FlagName='CouponAllDesignID'").Length > 0)
                            {
                                int designid = Convert.ToInt32(dtSetting.Select("FlagName='CouponAllDesignID'")[0]["Value"]);
                                designInfo = design.GetByID(designid);

                                if (designInfo != null)
                                {
                                    if (designInfo.ExpirationDate.Value > DateTime.Now)
                                    {
                                        img_coupon.Visible = true;
                                        img_coupon.Src = designInfo.CouponLocation;
                                        ViewState["designid"] = designid;
                                    }
                                }
                            }
                        }
                        else if (dtAnswer.Rows.Count > 0 && dtAnswer.Select("Answer<>'' and Answer is not null").Length > 0)
                        {
                            if (dtSetting.Select("FlagName='CouponPartialDesignID'").Length > 0)
                            {
                                int designid = Convert.ToInt32(dtSetting.Select("FlagName='CouponPartialDesignID'")[0]["Value"]);
                                designInfo = design.GetByID(designid);

                                if (designInfo != null)
                                {
                                    if (designInfo.ExpirationDate.Value > DateTime.Now)
                                    {
                                        img_coupon.Visible = true;
                                        img_coupon.Src = designInfo.CouponLocation;
                                        ViewState["designid"] = designid;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        PH_Error.Visible = true;
                        PH_Content.Visible = false;
                    }
                }
                else
                {
                    PH_Error.Visible = true;
                    PH_Content.Visible = false;
                }

            }
        }

        string GenerateCouponCode()
        {
            Random rnd = new Random();
            int couponCode = rnd.Next(10000000, 99999999);

            CouponRedeemDAL redeem = new CouponRedeemDAL();
            if (redeem.GetByCouponCode(couponCode.ToString()) == null)
            {
                return couponCode.ToString();
            }
            else
            {
                return GenerateCouponCode();
            }
            //return Utility.Encryption.Encrypt(DateTime.Now.Ticks.ToString());
        }

        protected void btn_Submit_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(reviewid))
            {
                ReviewDAL review = new ReviewDAL();
                ReviewInfo reviewInfo = review.GetByID(Convert.ToInt32(reviewid));

                if (reviewInfo != null)
                {
                    CouponDesignDAL design = new CouponDesignDAL();
                    CouponDesignInfo designInfo = design.GetByID(Convert.ToInt32(ViewState["designid"]));

                    if (designInfo != null)
                    {
                        CouponRedeemDAL redeem = new CouponRedeemDAL();
                        CouponRedeemInfo redeemInfo = new CouponRedeemInfo();
                        redeemInfo.UserGUID = reviewInfo.UserGUID;
                        redeemInfo.DesignID = designInfo.ListID;
                        redeemInfo.BroswerGUID = reviewInfo.BrowserGUID;
                        redeemInfo.Email = Request["email"];
                        redeemInfo.ReviewID = reviewInfo.ReviewID;
                        redeemInfo.CouponCode = GenerateCouponCode();
                        redeemInfo.IsRedeemed = 0;
                        redeemInfo.CouponCodeLocation = designInfo.CouponLocation;
                        try 
                        {
                            redeem.Add(redeemInfo);
                            WebUtility.SendCouponCodeEmail(redeemInfo.CouponCode, redeemInfo.Email, designInfo.CouponValue);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                    else
                    {
                        CouponRedeemDAL redeem = new CouponRedeemDAL();
                        CouponRedeemInfo redeemInfo = new CouponRedeemInfo();
                        redeemInfo.UserGUID = reviewInfo.UserGUID;
                        redeemInfo.BroswerGUID = reviewInfo.BrowserGUID;
                        redeemInfo.Email = Request["email"];
                        redeemInfo.ReviewID = reviewInfo.ReviewID;
                        redeemInfo.IsRedeemed = 0;
                        try
                        {
                            redeem.Add(redeemInfo);
                            WebUtility.SendReviewThankYouEmail(redeemInfo.Email);
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                    }
                }
                
                Response.Redirect("ThankYou.aspx", false);
            }
        }
    }
}